package com.ww.scm.entity.mat;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.ww.scm.constants.Constants;
import com.ww.scm.entity.AbstractEntity;
import com.ww.scm.entity.pub.Dictionary;
import com.ww.scm.repository.mat.AccountRepository;
import com.ww.scm.repository.mat.StorageRepository;

@Entity
@Table(name = "T_MAT_TRANSACTION_DETAIL")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
// 事物明细
public class TransactionDetail extends AbstractEntity{

	//业务事物
	private Transaction transaction;
	//事务明细类型:-1.投入 0.默认 1.产出
	private String type;	
	//物料
	private Dictionary materiel;
	//数量
	private Double amount=0.0;
	// 重量
	private Double weight=0.0;
	//单价
	private Double price=0.0;
	//金额
	private Double money=0.0;
	//单位
	private Dictionary unit;
	//帐目
	private Set<Account> account=new HashSet<Account>(0);
	//现存量
	private Storage storage;
	// 源位置
	private Dictionary originEntity;
	//目标位置
	private Dictionary targetEntity;
	// 状态
	private Dictionary state;	
	
	private Dictionary otherDic1;
	private Dictionary otherDic2;
	private Dictionary otherDic3;
	private Dictionary otherDic4;
	private Dictionary otherDic5;
	private Dictionary otherDic6;
	private Dictionary otherDic7;
	private Dictionary otherDic8;
	private Dictionary otherDic9;
	private Dictionary otherDic10;
	
	private String value1;
	private String value2;
	private String value3;
	private String value4;
	private String value5;
	private String value6;
	private String value7;
	private String value8;
	private String value9;
	private String value10;
	
	private Double double1;
	private Double double2;
	private Double double3;
	private Double double4;
	private Double double5;
	private Double double6;
	private Double double7;
	private Double double8;
	private Double double9;
	private Double double10;	
	
	public TransactionDetail() {
	}
	
	public TransactionDetail(String type) {
		this.type=type;
	}

	@Transient
	public void createOrUpdateStorage(StorageRepository storageRepository, String operator) {
		if (Constants.TRANSACTION_DETAIL_TYPE_IN_ID.equals(type)){
			this.storage = storageRepository.createOrUpdateStorage(this, false, operator);
		}else if (Constants.TRANSACTION_DETAIL_TYPE_OUT_ID.equals(type)){
			this.storage = storageRepository.createOrUpdateStorage(this, true, operator);
		}
	}

	@Transient
	public void createAccount(AccountRepository accountRepository,String operator) {
		if (Constants.TRANSACTION_DETAIL_TYPE_IN_ID.equals(type)){
			account.add(accountRepository.createAccount(this, false, operator));
		}else if (Constants.TRANSACTION_DETAIL_TYPE_OUT_ID.equals(type)){
			account.add(accountRepository.createAccount(this, true, operator));
		}
	}

	@Transient
	public void rollbackAccount(AccountRepository accountRepository,String operator) {
		if (Constants.TRANSACTION_DETAIL_TYPE_IN_ID.equals(type)){
			account.add(accountRepository.rollbackAccount(this,false, operator));
		}else if (Constants.TRANSACTION_DETAIL_TYPE_OUT_ID.equals(type)){
			account.add(accountRepository.rollbackAccount(this,true, operator));
		}
	}

	@Transient
	public void rollbackStorage(StorageRepository storageRepository,String operator) {
		if (Constants.TRANSACTION_DETAIL_TYPE_IN_ID.equals(type)){
			storageRepository.rollbackStorage(this,false, operator);
		}else if (Constants.TRANSACTION_DETAIL_TYPE_OUT_ID.equals(type)){
			storageRepository.rollbackStorage(this,true, operator);
		}
	}

	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
	@JoinColumn(name = "F_TRANSACTION_ID") 
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public Transaction getTransaction() {
		return transaction;
	}

	@Column(name = "F_TYPE_ID", nullable = false, length = 32)
	public String getType() {
		return type;
	}

	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
	@JoinColumn(name = "F_MATERIEL_ID") 
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public Dictionary getMateriel() {
		return materiel;
	}
	
	@Column(name = "F_AMOUNT", nullable = false, precision = 12, scale = 4)
	public Double getAmount() {
		return amount;
	}
	
	@Column(name = "F_WEIGHT", nullable = false, precision = 12, scale = 4)
	public Double getWeight() {
		return weight;
	}
	@Column(name = "F_PRICE",   precision = 12, scale = 4)
	public Double getPrice() {
		return price;
	}
	@Column(name = "F_MONEY",   precision = 12, scale = 4)
	public Double getMoney() {
		return money;
	}

	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
	@JoinColumn(name = "F_UNIT_ID") 
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public Dictionary getUnit() {
		return unit;
	}

	@OneToMany(mappedBy = "transactionDetail", fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
	@OrderBy("lastModifiedTime")
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public Set<Account> getAccount() {
		return account;
	}

	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
	@JoinColumn(name = "F_STORAGE_ID") 
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public Storage getStorage() {
		return storage;
	}

	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
	@JoinColumn(name = "F_ORIGIN_ENTITY_ID") 
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public Dictionary getOriginEntity() {
		return originEntity;
	}
	
	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
	@JoinColumn(name = "F_TARGET_ENTITY_ID") 
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public Dictionary getTargetEntity() {
		return targetEntity;
	}
	
	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
	@JoinColumn(name = "F_STATE_ID") 
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)	
	public Dictionary getState() {
		return state;
	}	

	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST) 
	@JoinColumn(name = "F_OTHER_DIC_ID1") 
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE) 
	public Dictionary getOtherDic1() {
		return otherDic1;
	}
	
	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST) 
	@JoinColumn(name = "F_OTHER_DIC_ID2") 
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE) 
	public Dictionary getOtherDic2() {
		return otherDic2;
	}
	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST) 
	@JoinColumn(name = "F_OTHER_DIC_ID3") 
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE) 
	public Dictionary getOtherDic3() {
		return otherDic3;
	}
	
	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST) 
	@JoinColumn(name = "F_OTHER_DIC_ID4") 
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE) 
	public Dictionary getOtherDic4() {
		return otherDic4;
	}
	
	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST) 
	@JoinColumn(name = "F_OTHER_DIC_ID5") 
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE) 
	public Dictionary getOtherDic5() {
		return otherDic5;
	}
	
	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST) 
	@JoinColumn(name = "F_OTHER_DIC_ID6") 
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE) 
	public Dictionary getOtherDic6() {
		return otherDic6;
	}
	
	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST) 
	@JoinColumn(name = "F_OTHER_DIC_ID7") 
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE) 
	public Dictionary getOtherDic7() {
		return otherDic7;
	}
	
	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST) 
	@JoinColumn(name = "F_OTHER_DIC_ID8") 
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE) 
	public Dictionary getOtherDic8() {
		return otherDic8;
	}
	
	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST) 
	@JoinColumn(name = "F_OTHER_DIC_ID9") 
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE) 
	public Dictionary getOtherDic9() {
		return otherDic9;
	}
	
	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST) 
	@JoinColumn(name = "F_OTHER_DIC_ID10") 
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE) 
	public Dictionary getOtherDic10() {
		return otherDic10;
	}
	
	@Column(name = "F_VALUE1", length = 500)
	public String getValue1() {
		return value1;
	}

	@Column(name = "F_VALUE2", length = 500)
	public String getValue2() {
		return value2;
	}
	@Column(name = "F_VALUE3", length = 500)
	public String getValue3() {
		return value3;
	}
	@Column(name = "F_VALUE4", length = 500)
	public String getValue4() {
		return value4;
	}
	
	@Column(name = "F_VALUE5", length = 500)
	public String getValue5() {
		return value5;
	}
	
	@Column(name = "F_VALUE6", length = 500)
	public String getValue6() {
		return value6;
	}
	
	@Column(name = "F_VALUE7", length = 500)
	public String getValue7() {
		return value7;
	}
	
	@Column(name = "F_VALUE8", length = 500)
	public String getValue8() {
		return value8;
	}
	
	@Column(name = "F_VALUE9", length = 500)
	public String getValue9() {
		return value9;
	}
	
	@Column(name = "F_VALUE10", length = 500)
	public String getValue10() {
		return value1;
	}
	
	@Column(name = "F_DOUBLE1",   precision = 12, scale = 4)
	public Double getDouble1() {
		return double1;
	}
	
	@Column(name = "F_DOUBLE2",   precision = 12, scale = 4)
	public Double getDouble2() {
		return double2;
	}
	
	@Column(name = "F_DOUBLE3",   precision = 12, scale = 4)
	public Double getDouble3() {
		return double3;
	}
	
	@Column(name = "F_DOUBLE4",   precision = 12, scale = 4)
	public Double getDouble4() {
		return double4;
	}
	
	@Column(name = "F_DOUBLE5",   precision = 12, scale = 4)
	public Double getDouble5() {
		return double5;
	}
	
	@Column(name = "F_DOUBLE6",   precision = 12, scale = 4)
	public Double getDouble6() {
		return double6;
	}
	
	@Column(name = "F_DOUBLE7",   precision = 12, scale = 4)
	public Double getDouble7() {
		return double7;
	}
	
	@Column(name = "F_DOUBLE8",   precision = 12, scale = 4)
	public Double getDouble8() {
		return double8;
	}
	
	@Column(name = "F_DOUBLE9",   precision = 12, scale = 4)
	public Double getDouble9() {
		return double9;
	}
	
	@Column(name = "F_DOUBLE10",   precision = 12, scale = 4)
	public Double getDouble10() {
		return double10;
	}
	
	public void setTransaction(Transaction transaction) {
		this.transaction = transaction;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void setMateriel(Dictionary materiel) {
		this.materiel = materiel;
	}

	public void setAmount(Double amount) {
		this.amount = amount;
	}

	public void setWeight(Double weight) {
		this.weight = weight;
	}
	
	public void setPrice(Double price) {
		this.price = price;
	}
	
	public void setMoney(Double money) {
		this.money = money;
	}

	public void setUnit(Dictionary unit) {
		this.unit = unit;
	}

	public void setAccount(Set<Account> account) {
		this.account = account;
	}

	public void setStorage(Storage storage) {
		this.storage = storage;
	}

	public void setOriginEntity(Dictionary originEntity) {
		this.originEntity = originEntity;
	}

	public void setTargetEntity(Dictionary targetEntity) {
		this.targetEntity = targetEntity;
	}

	public void setState(Dictionary state) {
		this.state = state;
	}
	
	public void setOtherDic1(Dictionary other1) {
		this.otherDic1 = other1;
	}
	
	public void setOtherDic2(Dictionary other2) {
		this.otherDic2 = other2;
	}
	public void setOtherDic3(Dictionary other3) {
		this.otherDic3 = other3;
	}
	
	public void setOtherDic4(Dictionary other4) {
		this.otherDic4 = other4;
	}
	
	public void setOtherDic5(Dictionary other5) {
		this.otherDic5 = other5;
	}
	
	public void setOtherDic6(Dictionary other6) {
		this.otherDic6 = other6;
	}
	
	public void setOtherDic7(Dictionary other7) {
		this.otherDic7 = other7;
	}
	
	public void setOtherDic8(Dictionary other8) {
		this.otherDic8 = other8;
	}
	
	public void setOtherDic9(Dictionary other9) {
		this.otherDic9 = other9;
	}
	
	public void setOtherDic10(Dictionary other10) {
		this.otherDic10 = other10;
	}

	public void setValue1(String value1) {
		this.value1 = value1;
	}
	
	public void setValue2(String value2) {
		this.value2 = value2;
	}

	public void setValue3(String value3) {
		this.value3 = value3;
	}

	public void setValue4(String value4) {
		this.value4 = value4;
	}

	public void setValue5(String value5) {
		this.value5 = value5;
	}

	public void setValue6(String value6) {
		this.value6 = value6;
	}

	public void setValue7(String value7) {
		this.value7 = value7;
	}

	public void setValue8(String value8) {
		this.value8 = value8;
	}

	public void setValue9(String value9) {
		this.value9 = value9;
	}

	public void setValue10(String value10) {
		this.value10 = value10;
	}
	
	public void setDouble1(Double double1) {
		this.double1 = double1;
	}
	
	public void setDouble2(Double double2) {
		this.double2 = double2;
	}

	public void setDouble3(Double double3) {
		this.double3 = double3;
	}

	public void setDouble4(Double double4) {
		this.double4 = double4;
	}

	public void setDouble5(Double double5) {
		this.double5 = double5;
	}

	public void setDouble6(Double double6) {
		this.double6 = double6;
	}

	public void setDouble7(Double double7) {
		this.double7 = double7;
	}

	public void setDouble8(Double double8) {
		this.double8 = double8;
	}

	public void setDouble9(Double double9) {
		this.double9 = double9;
	}

	public void setDouble10(Double double10) {
		this.double10 = double10;
	}
}
